<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <!-- 搜索/添加 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            @click="toAddPage"
          >添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表区域 -->
      <el-table
        :data="goodsList"
        border
        stripe
      >
        <el-table-column
          type="index"
          label="#"
        ></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
        ></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
          width="70px"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="70px"
        ></el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间"
          width="180px"
        >
          <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="140px"
        >
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="show_editDialog(scope.row)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 点击编辑按钮弹窗的对话框 -->
    <edit-dialog
      :clickBtn="dialog_edit_click"
      @confirm="dialog_edit_confirm"
      :dialogForm="editForm"
    ></edit-dialog>
  </div>
</template>

<script>
import goods from '../../dataSource/goods'

import { $confirm } from "../../utils/tools";
import _ from "lodash";
import editDialog from "./dialog/list_edit_dialog";
export default {
  components: {
    editDialog
  },
  created() {
    this.getGoodsList();
  },
  mounted() {},
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表数据
      goodsList: [],
      // 总条数
      total: 0,
      //  控制edit-dialog对话框显示隐藏
      dialog_edit_click: false,
      // edit-dialog对话框的表单数据
      editForm: {
        goods_name: "",
        goods_price: "",
        goods_number: "",
        goods_weight: ""
      }
    };
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList() {
      // 可以带查询参数，也可以不带查询参数
      // const { data: res } = await this.$http.get("goods", {
      //   params: this.queryInfo
      // });
      const { data: res }=goods;
      // if (res.meta.status !== 200) {
      //   return this.$message.error("获取商品列表");
      // }
      this.goodsList = res.goods;
      this.total = res.total;
    },
    // pagesize改变触发的函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    // 页码发生变化时触发的函数
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },
    // 点击商品删除
    async removeById(id) {
      const result = await $confirm(this);
      if (result !== "confirm") {
        return this.$message.info("取消删除");
      }
      const { data: res } = await this.$http.delete(`goods/${id}`);
      if (res.meta.status !== 200) {
        return this.$message.error("删除失败");
      }
      this.$message.success("删除成功");
      this.getGoodsList();
    },
    // 点击添加商品，跳转到添加商品页面
    toAddPage() {
      this.$router.push("./goods/add");
    },
    // 点击按钮展示edit对话框
    show_editDialog(row) {
      this.editForm = _.cloneDeep(row);
      // this.editForm.goods_name=row.goods_name;
      // this.editForm.goods_price=row.goods_price;
      // this.editForm.goods_number=row.goods_number;
      // this.editForm.goods_weight=row.goods_weight;
      this.dialog_edit_click = !this.dialog_edit_click;
    },
    // 点击edit对话框的确定按钮
    async dialog_edit_confirm(form) {
      const { data: res } = await this.$http.put(`goods/${form.goods_id}`, {
        goods_name: form.goods_name,
        goods_price: form.goods_price,
        goods_number: form.goods_number,
        goods_weight: form.goods_weight,
        goods_introduce: "",
        pics: {},
        attrs: []
      });
      if (res.meta.status !== 201) {
        return this.$message.error(res.meta.msg);
      }
      this.getGoodsList();
      this.$message.success("成功");
    }
  }
};
</script>

<style scoped lang="less">
.el-table {
  margin-top: 15px;
}
</style>
